package com.linwen.javase.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @ClassName _13CyclicBarrierThread
 * @Description
 * @Author lin
 * @Date 2022/4/22 上午12:06
 * @Version V1.0
 *
 * 当满足阻塞队列中线程个数之后，当前线程执行
 */
public class _13CyclicBarrierThread {
    private static final int NUM = 7;
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(NUM,new Thread(()->{
            System.out.println("满足条件，当前线程执行");
        }));

        for (int i = 0; i < 14; i++) {
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName()+" 线程到位");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread().getName()+" 线程结束");
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }

    }
}
